package com.example.myapplication1;

import androidx.appcompat.app.AppCompatActivity;

import android.content.ContentValues;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

    private EditText username;
    private EditText password;
    private Button register;
    private MyDatabaseHelper dbHelper;
    private Button ret;
    private Button modify;
    private Button delete;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        modify = (Button)findViewById(R.id.modify);
        dbHelper = new MyDatabaseHelper(this, "Userinfo.db", null, 2);
        username = (EditText) findViewById(R.id.username);
        password = (EditText) findViewById(R.id.password);
        register = (Button)findViewById(R.id.register);
        ret = (Button)findViewById(R.id.ret);
        delete = (Button)findViewById(R.id.delete);
        register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                int x=0;
                String u = username.getText().toString();
                String p = password.getText().toString();
                SQLiteDatabase db = dbHelper.getWritableDatabase();
                Cursor cursor = db.query("userinfo", null, null, null, null, null, null);
                ContentValues values = new ContentValues();
                SharedPreferences.Editor editor = getSharedPreferences("userinfo",MODE_PRIVATE).edit();
                editor.putString("username",u);
                editor.putString("password",p);
                editor.commit();
                values.put("username",u);
                values.put("password",p);
                if (cursor.moveToFirst()) {

                    do {
                        String username = cursor.getString(cursor.
                                getColumnIndex("username"));
                        String password = cursor.getString(cursor.
                                getColumnIndex("password"));

                        if(u.equals(username)){
                            Toast.makeText(RegisterActivity.this, "注册失败，已经有此用户名", Toast.LENGTH_SHORT).show();
                            x=1;
                            break;

                        }


                    } while (cursor.moveToNext());
                }
                cursor.close();

                if(x==0){
                    db.insert("userinfo", null, values);
                    values.clear();
                    Toast.makeText(RegisterActivity.this, "注册成功!", Toast.LENGTH_SHORT).show();
                    Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                    startActivity(intent);
                }




                dbHelper.getWritableDatabase();


            }
        });

        ret.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this, LoginActivity.class);
                startActivity(intent);
            }
        });
        modify.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this, ModifyActivity.class);
                startActivity(intent);
            }
        });
        delete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegisterActivity.this,DeleteActivity.class);
                startActivity(intent);
            }
        });
    }
}